package com.zhongyu.item.comsumer.service;

import com.zhongyu.comm.pojo.Cinema;
import com.zhongyu.comm.pojo.Image;
import com.zhongyu.comm.pojo.Item;
import com.zhongyu.comm.pojo.ItemType;
import com.zhongyu.item.comsumer.client.CinemaClient;
import com.zhongyu.item.comsumer.client.ImageClient;
import com.zhongyu.item.comsumer.client.ItemClient;
import com.zhongyu.item.comsumer.client.ItemTypeClient;
import com.zhongyu.item.comsumer.vo.FloorItemsVo;
import com.zhongyu.item.comsumer.vo.ItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("indexService")
public class IndexServiceImpl implements IndexService {

    @Resource
    private ItemClient itemClient;

    @Resource
    private ItemTypeClient itemTypeClient;

    @Resource
    private ImageClient imageClient;

    @Resource
    private CinemaClient cinemaClient;


    @Override
    public List<FloorItemsVo> searchFloorItems() {
        List<ItemType> itemTypes= itemTypeClient.findByKey();
        return itemTypes.stream().map(itemType -> {
            FloorItemsVo floorItemsVo=new FloorItemsVo();
            floorItemsVo.setFloor(itemType.getKey());
            floorItemsVo.setItemTypeId(itemType.getId());
            floorItemsVo.setItemTypeName(itemType.getItemType());
            List<Item>list =itemClient.findTopN(itemType.getId(),5);
            for (Item item:list){
                ItemVo itemVo = new ItemVo();
                BeanUtils.copyProperties(item,itemVo);
                itemVo.setStartDate(item.getStartTime());
                itemVo.setEndDate(item.getEndTime());
                Image image=imageClient.findOne(item.getId(),2,1);
                itemVo.setImgUrl(image.getImgUrl());
                Cinema cinema= cinemaClient.findById(item.getCinemaId());
                itemVo.setAddress(cinema.getAddress());
                itemVo.setAreaId(cinema.getAreaId());
                itemVo.setAreaName(cinema.getAreaName());
                floorItemsVo.getItems().add(itemVo);
            }
            return floorItemsVo;
        }).collect(Collectors.toList());
    }
}
